"use strict";
// import fs = require("fs")
// import path = require("path")
// import { SocketConnectionManager } from "./SocketConnectionManager";
// import { SocketClient } from "./SocketClient";
// import { ProtoUtils } from "../protocol/ProtoUtils";
// import { ProtoConfig } from "../protocol/ProtoConfig";
// import { Buffer } from "buffer";
// import { RetCodeDefine } from "../protocol/RetCodeDefine"
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocketConnection = void 0;
const Logger_1 = require("../../utils/Logger");
const ProtoConfig_1 = require("../protocol/ProtoConfig");
const ProtoUtils_1 = require("../protocol/ProtoUtils");
const SocketMsg_1 = require("./SocketMsg");
const SocketConnectionManager_1 = require("./SocketConnectionManager");
const SocketClientMsgHandler_1 = require("./SocketClientMsgHandler");
class SocketConnection {
    constructor(connection, ip) {
        this.id = 0;
        this.isDestroyed = false;
        this.connection = connection;
        SocketConnection.id++;
        this.ip = ip;
        this.id = SocketConnection.id;
        this.initConnect();
    }
    initConnect() {
        this.isDestroyed = false;
        this.isClosing = false;
        this.connection.on('message', (data) => {
            this.onClientMsgHandler(data);
        });
        this.interval = setInterval(() => {
            this.checkConnection();
        }, 1000);
    }
    checkConnection() {
        if (this.connection.readyState == SocketConnection.ReadyState.Close) {
            this.connection.terminate();
            this.destroy();
            return false;
        }
        return true;
    }
    gotoClose() {
        this.isClosing = true;
        this.timeId = setTimeout(() => {
            this.close(true);
        }, 1000);
    }
    close(isForce = false) {
        if (this.connection.readyState == SocketConnection.ReadyState.Open || isForce) {
            this.connection.terminate();
            this.destroy();
        }
    }
    onClientMsgHandler(data) {
        try {
            let byteArray = new Uint8Array(data);
            let totalPack = ProtoUtils_1.ProtoUtils.DeserializeBinary(ProtoConfig_1.ProtoConfig.PB_MESSAGE, byteArray);
            // Logger.log("onClientMsgHandler========", totalPack);
            let reqId = totalPack.getSeq();
            let protoType = totalPack.getHead();
            let protoData = totalPack.getData();
            let protoName = ProtoConfig_1.ProtoConfig.getProtoReqName(protoType);
            let msg = SocketMsg_1.SocketMsg.createSocketMsg(reqId, protoType, protoName, protoData);
            SocketClientMsgHandler_1.SocketClientMsgHandler.onEventSocketGata(this, msg);
        }
        catch (error) {
            Logger_1.Logger.error("onClientMsgHandler======error==", error);
        }
    }
    createSendPack(socketMsg) {
        let serialdata = socketMsg.protoData.serializeBinary();
        let totalPack = ProtoUtils_1.ProtoUtils.Create(ProtoConfig_1.ProtoConfig.PB_MESSAGE);
        totalPack.setSeq(socketMsg.reqId);
        totalPack.setHead(socketMsg.protoType);
        totalPack.setData(serialdata);
        return totalPack.serializeBinary();
    }
    createMsgToClient(reqId, protoName, respPack) {
        let protoType = ProtoConfig_1.ProtoConfig.getProtoResponeseType(protoName);
        let socketMsg = SocketMsg_1.SocketMsg.createSocketMsg(reqId, protoType, protoName, respPack);
        this.sendMsgToClient(socketMsg);
    }
    sendMsgToClient(socketMstg) {
        if (!this.checkConnection()) {
            return;
        }
        if (!this.isDestroyed) {
            let totalPack = this.createSendPack(socketMstg);
            let sendBuff = Buffer.from(totalPack);
            this.connection.send(sendBuff);
        }
        else {
            Logger_1.Logger.error("sendMsgToClient=error=", this.id, socketMstg);
        }
    }
    isLogin() {
        if (this.user) {
            return true;
        }
        else {
            return false;
        }
    }
    destroy() {
        if (!this.isDestroyed) {
            clearTimeout(this.timeId);
            clearInterval(this.interval);
            this.isDestroyed = true;
            SocketConnectionManager_1.SocketConnectionManager.instance.removeSocketConnection(this);
            Logger_1.Logger.log("socket connnect destroy==", SocketConnectionManager_1.SocketConnectionManager.instance.socketConnList.length);
        }
    }
}
exports.SocketConnection = SocketConnection;
SocketConnection.ReadyState = {
    Open: 1,
    Close: 3
};
SocketConnection.id = 0;
